<?php

include_once "src/controller/blmanagement/UserAccountManagement.php";
include_once "src/model/User.php";

try {
    
    print "Now testing Display All Users Requirement.\n";
    print "Tests the following functions or portions there of:\n";
    print "UserAccountManagement::getAllUsers() Exception catching not tested\n";
    print "UserDBManagement::findAllUsers() Exception catching not tested\n";
    print "DBConnection() Exception catching not tested\n";
    print "User()";
    print "DBConnection::execute() Exception catching not tested\n";
    print "User::setAll()\n";
    print "User::setUserId()\n";
    print "DBConnection::disconnect()\n\n";
    $userArray= UserAccountManagement::getAllUsers();
    $nextUserNo = 0;
    foreach($userArray as $user)
    {
       $name = $user->getName();
       if ($user->isAdmin()) print"$name is an Administrator\n";
       else if($user->isOwner()) print"$name is an Owner\n";
       else print"$name is a ordinary user\n";
       $test = UserAccountManagement::removeByUserId($user->getUserId());
       $userNo = $user->getUserId();
       $email = $user->getEmail();
       print "user # is $userNo and email is $email\n";
       if ($userNo>$nextUserNo) $nextUserNo=$userNo;
    }
    $nextUserNo++;
    $name="Sam O'Leary";
    $password="oCC9ihc9";
    $email="so'leary@gmail.com";
    $userType=1;
    $user = new User();
    $user->setType($userType);
    $user->setEmail($email);
    $user->setPswd($password);
    $user->setName($name);
    print "\nTest requirement Add User no faults\n";
    $test= UserAccountManagement::addUser($user); //user should be able to add
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created.\n";
    }
    print "Test requirement Add User with same password\n";
    $user->setAll("same email", 1, "pswd", $email); //set user with same email
    $test= UserAccountManagement::addUser($user); //user should not able to add
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created.\n";
    }
    print "Test requirement Delete User no faults\n";
    $test = UserAccountManagement::removeByUserId($nextUserNo); //user should be able to delete
    if ($test==FALSE)
    {
        echo "User does not exist!!\n";
    }
    else{
        echo "User deleted.\n";
    }
    print "Test requirement Delete User who no longer exists in database\n";
     $test = UserAccountManagement::removeByUserId($nextUserNo); //user already deleted
    if ($test==FALSE)
    {
        echo "User does not exist!!\n";
    }
    else{
        echo "User deleted.\n";
    }
    print "Test requirement Add User password for user different then in authorization db\n";
    $name="Sam O'Leary";
    $user->setName($name);
    $pswd="x7GU2995";
    $user->setpswd($pswd);
    $test= UserAccountManagement::addUser($user); //user should be able to add
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created\n";
    }
    print "Test requirement Add 2 Users back\n";
    $pswd="oCC9ihc9";
    $user->setpswd($pswd);
    $test= UserAccountManagement::addUser($user); //user should be able to add
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created\n";
    }

    $name="Tom Smith";
    $password="1234";
    $email="tsmith@gmail.com";
    $userType=3;
    $user = new User();
    $user->setAll($name,$userType,$password,$email);
    $test= UserAccountManagement::addUser($user); //user should be able to add
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created\n";
    }
    print "Test requirement Update User Account user not in system\n";

    $userNo=6;
    $user->setUserId($userNo);
    $test= UserAccountManagement::changeUserAccount($user); //user No 6 does not exist
    if ($test==FALSE)
    {
        echo "User with id $userNo does not exist in system!!\n";
    }
    else{
        echo "User with ID $userNo changed.\n";
    }
    print "Test requirement Update User Account user able to change their email.\n";

    $nextUserNo=$nextUserNo+2;
    $user->setUserId($nextUserNo);
    $email="tbsmith@gmail.com";
    $user->setEmail($email);
    $test= UserAccountManagement::changeUserAccount($user); //user should be able to change email
    if ($test==FALSE)
    {
        echo "User with id $nextUserNo does not exist in system!!\n";
    }
    else{
        echo "User with ID $nextUserNo changed.\n";
    }
    print "Test requirement Update User Account user not able to change their email to existing one.\n";
    $email="so'leary@gmail.com";
    $user->setEmail($email);
    $test= UserAccountManagement::changeUserAccount($user); //user should not be able to change email to existing
    if ($test==FALSE)
    {
        echo "User with email $email already exists in system!!\n";
    }
    else{
        echo "User with ID $nextUserNo changed.\n";
    }
    print "Test requirement Update User Account user able to change their email.\n";
    $email="tbsmith@gmail.com";
    $user->setEmail($email);
      $name="Tim Smith";
    $user->setName($name);
    $test= UserAccountManagement::changeUserAccount($user); //user should not be able to change email to existing
    if ($test==FALSE)
    {
        echo "User with email $email already exists in system!!\n";
    }
    else{
        echo "User with ID $userNo changed.\n";
    }
      print "Test requirement Add User Account user unable to add with existing email.\n";
     $name="Jim Jones";
    $password="onV!#lBx";
    $email="JJones@gmail.com";
    $userType=2;
    $user->setName($name);
    $user->setPswd($password);
    $user->setType($userType);
    $test= UserAccountManagement::addUser($user);
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created\n";
    }
     print "Test requirement Add User Account user able to add.\n";
    $user->setEmail($email);
    $test= UserAccountManagement::addUser($user);
    if ($test==FALSE)
    {
        echo "User exists!!\n";
    }
    else{
        echo "New user created\n";
    }
    $email="tbsmith@gmail.com";
    $user = UserAccountManagement::getUserByEmail($email);
    $id= $user->getUserId();
    $name= $user->getName();
    $type= $user->getType();
    $pswd= $user->getPswd();
    print "User with email:  $email is $name whose id is $id and password is $pswd\n";
    if ($user->isAdmin()) print"$name in an Administrator\n";
    else if($user->isOwner()) print"$name is an Owner\n";
    else print"$name is a ordinary user\n";
    $test = AuthorizationAccountManagement::login($email,$pswd);
    if ($test)
    {
        print"user with email $email is logged in.\n";
    }
    else
    {
        print"user with email $email and password $pswd could not be authenticated.\n";
    }
    $email= 'JJones@gmail.com';
    $pswd="onV!#lBx";
    $test = AuthorizationAccountManagement::login($email,$pswd);
    if ($test)
    {
        print"user with email $email is logged in.\n";
    }
    else
    {
        print"user with email $email and password $pswd could not be authenticated.\n";
    }
    $email= "so'leary@gmail.com";
    $pswd="oCC9ihc9";
    $test = AuthorizationAccountManagement::login($email,$pswd);
    if ($test)
    {
        print"user with email $email is logged in.\n";
    }
    else
    {
        print"user with email $email and password $pswd could not be authenticated.\n";
    }
    $email="tbsmith@gmail.com";
    //AuthorizationAccountManagement::forgotPassword($email);
    } catch (Exception $e) {
        $e->getMessage();
        echo $e;



}

?>
